<template>
  <el-drawer
    :wrapperClosable="false"
    direction="rtl"
    custom-class="demo-drawer"
    :title="title"
    ref="drawer"
    size="40%"
    :visible.sync="dialogFormVisible"
  >
    <div class="demo-drawer__content">
      <el-form ref="form" label-width="40" :model="form" :rules="rules">
        <el-form-item label="所在区域" prop="areaId">
          <el-select v-model="form.areaId" placeholder="区域">
            <el-option v-for="item in areaList" :key="item.id" :label="item.area" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="初高中" prop="schoolType">
          <el-select v-model="form.schoolType" placeholder="初高中" @change="changeTag()">
            <el-option v-for="item in Dpmentlist" :key="item.value" :label="item.dpName" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学校类型:" prop="schoolTag">
          <el-select v-if="form.schoolType == 0" v-model="form.schoolTag" placeholder="学校类型">
            <el-option v-for="item in schoolTagList" :key="item.keyName" :label="item.memo" :value="item.keyName"></el-option>
          </el-select>

          <el-select v-if="form.schoolType == 1" v-model="form.schoolTag" placeholder="学校类型">
            <el-option v-for="item in schoolTagList1" :key="item.keyName" :label="item.memo" :value="item.keyName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" autocomplete="off" />
        </el-form-item>
        <el-form-item v-if="form.schoolType == 1" label="统招录取分数线" :prop="form.schoolType == 1 ? 'unifiedScore' : ''">
          <el-input placeholder="0-790" v-model="form.unifiedScore" autocomplete="off" />
        </el-form-item>
        <el-form-item v-if="form.schoolType == 1" label="指标录取分数线" :prop="form.schoolType == 1 ? 'targetScore' : ''">
          <el-input placeholder="0-790" v-model="form.targetScore" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button @click="close" type="warning">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import { GetDictListWithKeyName } from '@/api/table'
  import { AddSchool, SetSchool } from '@/api/user'

  import { GetAreaList } from '@/api/userManagement'
  import VabUpload from '@/components/VabUpload'

  export default {
    components: {
      VabUpload,
    },
    name: 'TableEdit',
    data() {
      return {
        Dpmentlist: [
          { dpName: '初中', value: 0 },
          { dpName: '高中', value: 1 },
        ],
        areaList: [],
        schoolTagList: [],
        schoolTagList1: [],
        form: {
          schoolType: 0,
          schoolTag: '',
          areaId: '',
          unifiedScore: 0,
          targetScore: 0,
        },
        rules: {
          schoolType: [{ type: 'number', required: true, trigger: 'blur', message: '请选择学校类型' }],
          areaId: [{ type: 'number', required: true, trigger: 'blur', message: '请选择学校区域' }],
          schoolTag: [{ type: 'string', required: true, trigger: 'blur', message: '请选择学校类型' }],
          schoolName: [{ type: 'string', required: true, trigger: 'blur', message: '请输入学校名称' }],
          unifiedScore: [{ required: true, trigger: 'blur', message: '请输入统招录取分数线' }],
          targetScore: [{ required: true, trigger: 'blur', message: '请输入指标录取分数线' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      changeTag() {
        this.form.schoolTag = ''
      },
      async showEdit(row) {
        const { data } = await GetAreaList()
        this.areaList = data
        const data1 = await GetDictListWithKeyName({ keyName: '' })
        this.schoolTagList = data1.data.filter((item) => item.disClass == 1)
        this.schoolTagList1 = data1.data.filter((item) => item.disClass == 2)
        if (!row) {
          this.title = '新增学校'
        } else {
          this.title = '编辑学校'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      save() {
        console.log(this.form)

        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = this.title === '新增学校' ? await AddSchool(this.form) : await SetSchool(this.form)
            this.$baseMessage(msg, 'success')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  ::v-deep .demo-drawer__content {
    height: 100%;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    .demo-drawer__footer {
      bottom: 0;
    }
  }
</style>
